package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.Member;
import com.ly.pojo.dto.MemberAddDTO;
import com.ly.pojo.dto.MemberDTO;
import com.ly.pojo.vo.MemberBelongVO;
import com.ly.service.MemberService;
import com.ly.service.UserService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : 黑渊白花
 * @ClassName MemberController
 * @date : 2025/2/10 21:47
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result list(){
        List<Member> list = memberService.list();
        return Result.success(list);
    }

    @GetMapping("/Allpage")
    public Object Allpage( MemberDTO memberDTO){
        log.info("{}",memberDTO);
        IPage<MemberBelongVO> allMemberPage = memberService.getAllMemberPage(memberDTO);
        return Result.success(allMemberPage);
    }

    @GetMapping("/page")
    public Object page( MemberDTO memberDTO){
        log.info("{}",memberDTO);
        IPage<MemberBelongVO> page=null;
        page = memberService.getUserMemberPage(memberDTO);
        return Result.success(page);
    }

    @GetMapping("/isJoinThisTeam")
    @ExcludeToken
    public Result isJoinThisTeam( MemberAddDTO memberAddDTO){
        Boolean joinThisTeam = memberService.isJoinThisTeam(memberAddDTO);
        if(joinThisTeam){
            return Result.success(1);
        }
        return Result.success(0);
    }

    @GetMapping("/getTeamMember/{id}")
    @ExcludeToken
    public Result getTeamMember(@PathVariable Integer id){
        List<MemberBelongVO> teamMember = memberService.getTeamMember(id);
        if(teamMember.size()==0){
            return Result.warn("出错啦");
        }
        return Result.success(teamMember);
    }

    @PostMapping("/save")
    public Result save(@RequestBody MemberAddDTO memberAddDTO){
        log.info("{}",memberAddDTO);
        Boolean b = memberService.addMember(memberAddDTO);
        if(b==false){
            return Result.warn("没有该学生或者该学生是社长");
        }
        return Result.success("添加成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        Member byId = memberService.getById(id);
        if(byId==null){
            return Result.warn("系统错误");
        }
        //删除成员同时将所属社团人数-1
         memberService.delete(byId);
         return Result.success();
    }

    @PostMapping ("/exitTeam")
    public Result exitTeam(@RequestBody Member member){
        return memberService.exitTeam(member);

    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Member byId = memberService.getById(id);
        return Result.success(byId);
    }



    //修改成得到男女人数
    @GetMapping("/getMemCollege/{teamId}")
    public Result getMemCollege(@PathVariable Integer teamId){
        //删除成员同时将所属社团人数-1
        List<Map<String, Object>> memCollege = memberService.getMemCollege(teamId);
        return Result.success(memCollege);
    }
}
